package com.xiaotu.common.util;

public class DBArrayUtils {

	public static void main(String[] args) {
		System.out.println(DBArrayUtils.getArrayString("{    ,宁财神，     小舟，       何员外,  }"));
	}

	@SuppressWarnings("null")
	public static String getArrayString(String str) {
		if (str == null && str.equals("{}") && str.equals("") && str.equals("{null}")) {
			return null;
		}
		//要替换成","的
		String[] s1 = new String[]{"，","。","；",";","、","/",":","："};
		//要替换成""的	
		String[] s2 = new String[]{"\"","《","》","<",">","“","”","？","?","@","#","$","^","*","&"};
		//要替换成","的
		String[] s3 = new String[]{"》《","><","”“","\"\"","“”"};
		
		if(str.contains("&middot;")){
			str=str.replace("&middot;", "·");
		}
		if(str.contains("&nbsp;")){
			str=str.replace("&nbsp;", "");
		}
		for(int i=0;i<s3.length;i++){
			if(str.replace("  ", "").contains(s3[i])){
				str = str.replace("  ", "");
				str = str.replace(" ", "");
				str = str.replace(s3[i], ",");
			}              
			if(str.replace(" ", "").contains(s3[i])){
				str = str.replace("  ", "");
				str = str.replace(" ", "");
				str = str.replace(s3[i], ",");
			}
		}
		 
		for(int i=0;i<s1.length;i++){
			if(str.replace("  ", "").contains(s1[i])){
				str = str.replace("  ", "");
				str = str.replace(" ", "");
				str = str.replace(s1[i], ",");
			}
			if(str.replace(" ", "").contains(s1[i])){
				str = str.replace("  ", "");
				str = str.replace(" ", "");
				str = str.replace(s1[i], ",");
			}
		}
		for(int i=0;i<s2.length;i++){
			if(str.replace("  ", "").contains(s2[i])){
			//	str = str.replace("  ", "");
			//	str = str.replace(" ", "");
				str = str.replace(s2[i], "");
			}
			if(str.replace(" ", "").contains(s2[i])){
			//	str = str.replace("  ", "");
			//	str = str.replace(" ", "");
				str = str.replace(s2[i], "");
			}
		}
		//空格
		if(str.contains(" ")){
			//去掉中文空格
			str = str.replace("  ", "");
			String[] total = str.trim().split(" ");
			String strOne = "";
			for(int i=0;i<total.length;i++){
				if(!total[i].trim().contains(",")){
					if(total[i].trim().equals("") || total[i].trim().equals("") || total[i].trim().equals("}")){
						if(total[i].trim().equals("}")){
							strOne += total[i].trim().replace("  ", "");
							continue;
						}
						continue;
					}
					strOne += total[i].trim()+",";
				}else{
					strOne += total[i].trim();
				}
			}
			if(str.endsWith("}")){
				str = strOne.substring(0,strOne.length()-2)+"}";
			}else{
				str = strOne.substring(0,strOne.length()-1);
			}
		}
						   
		while(str.contains(" ") || str.contains("  ") || str.contains("　") || str.contains(" ")){
			str = str.replace(" ", "").replace("  ", "").replace("　", "").replace(" ", "");
		}
		
		while(str.contains(",,")){
			str = str.replace(",,", ",");
		}
		
		while(str.contains("[") && str.contains("]")){
			int i = str.indexOf("[");
			int j = str.indexOf("]");
			str = str.substring(0,i)+ str.substring(j+1);
		}
		
		if(str.startsWith(",")){
			str = str.substring(1);
		}
		if(str.startsWith("{,")){
			str = "{"+str.substring(2);
		}
		if(str.endsWith(",")){
			str = str.substring(0, str.length()-1);
		}
		if(str.endsWith(",}")){
			str = str.substring(0, str.length()-2)+"}";
		}
		if(str.endsWith(",等}")){
			str = str.substring(0, str.length()-3)+"}";
		}
		String[] arrayStr = str.split(",");
		
		if(arrayStr.length >= 2){
			int a = arrayStr[0].replace("{", "").trim().length();
			int b = arrayStr[1].replace("}", "").trim().length();
			if(a==1 && b==1){
				str = str.replaceFirst("\\,", "");
			}
		}
		
		//最后判断是否带有外括号
//		if(!str.startsWith("{")){
//			str = "{"+str;
//		}
//		if(!str.endsWith("}")){
//			str = str+"}";
//		}

		return str;

	}
	
	public static String getString(String str){
		if(str != null && !str.equals("{}") && !str.equals("") && !str.equals("{null}")){
			//要替换成","的
			String[] s1 = new String[]{"，","。","；",";","、","/",":","："};
			//要替换成""的	
			String[] s2 = new String[]{"\"","《","》","<",">","“","”","？","?","@","#","$","^","=","*","&"};
			//要替换成","的
			String[] s3 = new String[]{"》《","><","”“","\"\"","“”"};
			
			for(int i=0;i<s3.length;i++){
				if(str.replace("  ", "").contains(s3[i])){
					str = str.replace("  ", "");
					str = str.replace(" ", "");
					str = str.replace(s3[i], ",");
				}              
				if(str.replace(" ", "").contains(s3[i])){
					str = str.replace("  ", "");
					str = str.replace(" ", "");
					str = str.replace(s3[i], ",");
				}
			}
			 
			for(int i=0;i<s1.length;i++){
				if(str.replace("  ", "").contains(s1[i])){
					str = str.replace("  ", "");
					str = str.replace(" ", "");
					str = str.replace(s1[i], ",");
				}
				if(str.replace(" ", "").contains(s1[i])){
					str = str.replace("  ", "");
					str = str.replace(" ", "");
					str = str.replace(s1[i], ",");
				}
			}
			for(int i=0;i<s2.length;i++){
				if(str.replace("  ", "").contains(s2[i])){
				//	str = str.replace("  ", "");
				//	str = str.replace(" ", "");
					str = str.replace(s2[i], "");
				}
				if(str.replace(" ", "").contains(s2[i])){
				//	str = str.replace("  ", "");
				//	str = str.replace(" ", "");
					str = str.replace(s2[i], "");
				}
			}
			//空格
			if(str.contains(" ")){
				//去掉中文空格
				str = str.replace("  ", "");
				String[] total = str.trim().split(" ");
				String strOne = "";
				for(int i=0;i<total.length;i++){
					if(!total[i].trim().contains(",")){
						if(total[i].trim().equals("") || total[i].trim().equals("") || total[i].trim().equals("}")){
							if(total[i].trim().equals("}")){
								strOne += total[i].trim().replace("  ", "");
								continue;
							}
							continue;
						}
						strOne += total[i].trim()+",";
					}else{
						strOne += total[i].trim();
					}
				}
				if(str.endsWith("}")){
					str = strOne.substring(0,strOne.length()-2)+"}";
				}else{
					str = strOne.substring(0,strOne.length()-1);
				}
			}
							   
			while(str.contains(" ") || str.contains("  ") || str.contains("　") || str.contains(" ")){
				str = str.replace(" ", "").replace("  ", "").replace("　", "").replace(" ", "");
			}
			
			while(str.contains(",,")){
				str = str.replace(",,", ",");
			}
			
			while(str.contains("[") && str.contains("]")){
				int i = str.indexOf("[");
				int j = str.indexOf("]");
				str = str.substring(0,i)+ str.substring(j+1);
			}
			
			if(str.startsWith(",")){
				str = str.substring(1);
			}
			if(str.startsWith("{,")){
				str = "{"+str.substring(2);
			}
			if(str.endsWith(",")){
				str = str.substring(0, str.length()-1);
			}
			if(str.endsWith(",}")){
				str = str.substring(0, str.length()-2)+"}";
			}
			if(str.endsWith(",等}")){
				str = str.substring(0, str.length()-3)+"}";
			}
			String[] arrayStr = str.split(",");
//			if(arrayStr.length == 2){
//				int a = arrayStr[0].trim().length();
//				int b = arrayStr[1].trim().length();
//				if(a==2 && b==2){
//					str = str.replace(",", "");
//				}
//			}
			
			if(arrayStr.length >= 2){
				int a = arrayStr[0].replace("{", "").trim().length();
				int b = arrayStr[1].replace("}", "").trim().length();
				if(a==1 && b==1){
					str = str.replaceFirst("\\,", "");
				}
			}
			return str;
		}else{
			return null;
		}
	}
}
